package jude.java.money.migrate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jude.java.util.Utils;

import org.apache.commons.lang.StringUtils;


public class Product {

	private String id;
	private String name;
	private String sku;
	private String price;
	private String specialPrice;
	private int qty;
	private String shortDescription;
	private String description;
	private boolean status;
	private String addedDate;
	private int weight;
	private List<String> images;
	private List<String> categories;
	private Map<String, String> attributes;
	
	
	private List<CustomOption> options;
	private Object[] record;
	
	// 以下为参数
	
	
	private Product() {
	}
	
	public void addOptions(CustomOption... options) {
		for (int i = 0; i < options.length; i++) {
			CustomOption option = options[i];
			this.options.add(option);
		}
	}
	
	public static Product getDefault() {
		Product p = new Product();
		p.options = new ArrayList<CustomOption>();
		p.images = new ArrayList<String>();
		p.categories = new ArrayList<String>();
		return p;
	}
	
	public Product fromRecord(Object[] record) {
		Product p = this;
		if (p.record != null)
			return p;
		p.record = record;
		
		p.id = v(record[0]);
		p.qty = Utils.getInteger(record[1], 0);
		p.sku = v(record[2]);
		p.images = new ArrayList<String>();
		String image = v(record[3]);
		if(StringUtils.isNotEmpty(image))
			p.images.add(image);
		p.price = v(record[4]);
		p.addedDate = v(record[5]);
		p.weight = Utils.getInteger(v(record[6]), 0);
		p.status = Boolean.TRUE.equals(record[7]);
		int pac = 8;
		p.name = v(record[pac]);
		p.description = v(record[pac + 1]);
		//master_categories_name
		p.specialPrice = v(record[pac + 3]);
		//master_categories_id
		//manufacturers_name
		return p;
	}
	
	private static String v(Object o) {
		if (o == null)
			return null;
		return String.valueOf(o);
	}
}
